/*
 * Created by Jaren at 2021/10/29 15:55
 */
import 'dart:typed_data';

import 'package:inmotion_protocol/common/compact.dart';
import 'package:inmotion_protocol/common/product_model.dart';
import 'package:inmotion_protocol/common/product_version.dart';
import 'package:inmotion_protocol/lorin/bean/scooter/scooter_settings.dart';
import 'package:inmotion_protocol/util/byte_util.dart';

class A1Settings extends ScooterSettings {

    A1Settings(ProductModel model, VersionWrapper version) : super(model, version);

  @override
    void initCompact() {
        compact = DefaultA1Settings();
    }

    @override
    String toString() {
        return 'A1Settings{compact: $compact, zeroStartupState: $zeroStartupState, cruiseState: $cruiseState, '
            'speedUnit: $speedUnit, ridingMode: $ridingMode, lockMode: $lockMode, safeSpeedState: $safeSpeedLimitState, '
            'accelerateForce: $accelerateForce, brakeForce: $brakeForce, limitSpeed: $limitSpeed}';
    }
}

class DefaultA1Settings extends Compact<A1Settings> {
    @override
    void fromBytes(A1Settings output, Uint8ClampedList bytes, int offset) {
        output.zeroStartupState = ByteUtil.bitValue(bytes[offset], 0, 1);
        output.cruiseState = ByteUtil.bitValue(bytes[offset], 1, 1);
        output.speedUnit = ByteUtil.bitValue(bytes[offset], 2, 1);
        output.ridingMode = output.toRidingMode(ByteUtil
            .bitValue(bytes[offset], 3, 3)
            .value);
        output.lockMode = ByteUtil.bitValue(bytes[offset], 6, 1);
        // 剩余1bit保留
        offset++;
        // 保留3bit
        output.safeSpeedLimitState = ByteUtil.bitValue(bytes[offset], 3, 1);
        // 剩余4bit保留
        offset++;
        output.accelerateForce = ByteUtil.bytes2Uint8Le(bytes, offset);
        offset++;
        output.brakeForce = ByteUtil.bytes2Uint8Le(bytes, offset);
        offset++;
        output.limitSpeed = ByteUtil.bytes2Uint16Le(bytes, offset);
    }

    @override
    int length() {
        return 6;
    }
}
